package com.ruoyi.cm.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cm.domain.vo.CmBsStartAdvanceVo;
import com.ruoyi.cm.domain.bo.CmBsStartAdvanceBo;
import com.ruoyi.cm.service.ICmBsStartAdvanceService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 开工预付款
 *
 * @author zyb
 * @date 2024-09-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cm/bsStartAdvance")
public class CmBsStartAdvanceController extends BaseController {

    private final ICmBsStartAdvanceService iCmBsStartAdvanceService;

    /**
     * 查询开工预付款列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<CmBsStartAdvanceVo> list(CmBsStartAdvanceBo bo, PageQuery pageQuery) {
        return iCmBsStartAdvanceService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询开工预付款列表
     */
    @SaCheckLogin
    @GetMapping("/listData")
    public R<List<CmBsStartAdvanceVo>>  listData(CmBsStartAdvanceBo bo) {
        return R.ok(iCmBsStartAdvanceService.listData(bo));
    }

    /**
     * 导出开工预付款列表
     */
    @SaCheckLogin
    @Log(title = "开工预付款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmBsStartAdvanceBo bo, HttpServletResponse response) {
        List<CmBsStartAdvanceVo> list = iCmBsStartAdvanceService.queryList(bo);
        ExcelUtil.exportExcel(list, "开工预付款", CmBsStartAdvanceVo.class, response);
    }

    /**
     * 获取开工预付款详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<CmBsStartAdvanceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iCmBsStartAdvanceService.queryById(id));
    }

    /**
     * 新增开工预付款
     */
    @SaCheckLogin
    @Log(title = "开工预付款", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CmBsStartAdvanceBo bo) {
        return toAjax(iCmBsStartAdvanceService.insertByBo(bo));
    }

    /**
     * 批量保存
     */
    @SaCheckLogin
    @Log(title = "开工预付款", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveBatch")
    public R<Void> saveBatch( @RequestBody List<CmBsStartAdvanceBo> boList) {
        return toAjax(iCmBsStartAdvanceService.saveBatch(boList));
    }

    /**
     * 修改开工预付款
     */
    @SaCheckLogin
    @Log(title = "开工预付款", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CmBsStartAdvanceBo bo) {
        return toAjax(iCmBsStartAdvanceService.updateByBo(bo));
    }

    /**
     * 删除开工预付款
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "开工预付款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCmBsStartAdvanceService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    // 标段关联删除
    @SaCheckLogin
    @GetMapping("/removeQs")
    public R<Void>  removeQs(CmBsStartAdvanceBo bo) {
        iCmBsStartAdvanceService.removeAdvance(bo);
//        iCmBsStartAdvanceService.removeQs(bo);
        return R.ok();
    }
}
